﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.Data;

namespace ProjectStudy.Ajax
{
    /// <summary>
    /// Ajax_Practice 的摘要说明
    /// </summary>
    public class Ajax_Practice : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action=context.Request["action"];
            switch (action)
            {
                case "getInt": getInt(context);
                    break;
                case "getFloat": getFloat(context);
                    break;
                case "getDouble": getDouble(context);
                    break;
                case "getString": getString(context);
                    break;
                case "getBool": getBool(context);
                    break;
                case "getList": getList(context);
                    break;
                case "getDictionary": getDictionary(context);
                    break;
                case "getDataTable": getDataTable(context);
                    break;
            }
        }

        private void getList(HttpContext context)
        {
            List<string> list=new List<string>();
            list.Add("张三");
            list.Add("李四");
            context.Response.Write(JsonConvert.SerializeObject(list));
        }

        private void getDataTable(HttpContext context)
        {
            string sql = "select * from class1";
            DataTable dt = DBAccess.ExecuteDataTable(sql);
            context.Response.Write(JsonConvert.SerializeObject(dt));
        }

        private void getDictionary(HttpContext context)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("1","cxf");
            dic.Add("2", "zs");
            context.Response.Write(JsonConvert.SerializeObject(dic));
        }

        private void getBool(HttpContext context)
        {
            bool a = false;
            context.Response.Write(a);
        }

        private void getString(HttpContext context)
        {
            string a = "fly";
            context.Response.Write(a);
        }

        private void getDouble(HttpContext context)
        {
            double a = 1.1111;
            context.Response.Write(a);
        }

        private void getFloat(HttpContext context)
        {
            float a = 1.11f;
            context.Response.Write(a);
        }

        private void getInt(HttpContext context)
        {
            int a = 1;
            context.Response.Write(a);
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}